package com.lgs7907.simplestockinfo;

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.concurrent.locks.ReentrantLock;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class StockDb extends SQLiteOpenHelper {
	private final static String DATABASE_NAME = "Stocks.db";
	private final static int DATABASE_VERSION = 1;
	private final static String TABLE_NAME = "base_table";
	public final static String STOCK_CODE = "stock_code";// 数据库中加入此字段
	public final static String STOCK_NAME = "stock_name";// 0：股票名字；

	public final static String STOCK_TODAY_PRICE = "stock_today_price";// 1：今日开盘价；
	public final static String STOCK_LASTDAY_PRICE = "stock_lastday_price";// 2：昨日收盘价；
	public final static String STOCK_CURRENT_PRICE = "stock_current_price";// 3：当前价格；
	public final static String STOCK_HEIGEST_PRICE = "stock_heigest_price";// 4：今日最高价；
	public final static String STOCK_LOWEST_PRICE = "stock_lowest_price";// 5：今日最低价；
	public final static String STOCK_BUY_PRICE0 = "stock_buy_price0";// 6：竞买价，即“买一”报价；
	public final static String STOCK_SALE_PRICE0 = "stock_sale_price0";// 7：竞卖价，即“卖一”报价；
	public final static String STOCK_STADE_TOTAL = "stock_stade_total";// 8：成交的股票数；
	public final static String STOCK_STADE_MONEY = "stock_stade_money";// 9：成交金额；
	public final static String STOCK_BUY_PRICE1 = "stock_buy_price1";// 10：“买一”申请4695股，即47手；
	public final static String STOCK_BUY_NUM1 = "stock_buy_num1";// 11：“买一”报价；
	public final static String STOCK_BUY_PRICE2 = "stock_buy_price2";// 12：”57590″，“买二”
	public final static String STOCK_BUY_NUM2 = "stock_buy_num2";// 13：”26.90″，“买二”
	public final static String STOCK_BUY_PRICE3 = "stock_buy_price3";// 14：”14700″，“买三”
	public final static String STOCK_BUY_NUM3 = "stock_buy_num3";// 15：”26.89″，“买三”
	public final static String STOCK_BUY_PRICE4 = "stock_buy_price4";// 16：”14300″，“买四”
	public final static String STOCK_BUY_NUM4 = "stock_buy_num4";// 17：”26.88″，“买四”
	public final static String STOCK_BUY_PRICE5 = "stock_buy_price5";// 18：”15100″，“买五”
	public final static String STOCK_BUY_NUM5 = "stock_buy_num5";// 19：”26.87″，“买五”
	public final static String STOCK_SALE_PRICE1 = "stock_sale_price1";// 20：”3100″，“卖一”申报3100股，即31手；
	public final static String STOCK_SALE_NUM1 = "stock_sale_num1";// 21：”26.92″，“卖一”报价
	public final static String STOCK_SALE_PRICE2 = "stock_sale_price2";// 22：”57590″，“卖二”
	public final static String STOCK_SALE_NUM2 = "stock_sale_num2";// 23：”26.90″，“卖二”
	public final static String STOCK_SALE_PRICE3 = "stock_sale_price3";// 24：”14700″，“卖三”
	public final static String STOCK_SALE_NUM3 = "stock_sale_num3";// 25：”26.89″，“卖三”
	public final static String STOCK_SALE_PRICE4 = "stock_sale_price4";// 26：”14300″，“卖四”
	public final static String STOCK_SALE_NUM4 = "stock_sale_num4";// 27：”26.88″，“卖四”
	public final static String STOCK_SALE_PRICE5 = "stock_sale_price5";// 28：”15100″，“卖五”
	public final static String STOCK_SALE_NUM5 = "stock_sale_num5";// 29：”26.87″，“卖五”
	public final static String STOCK_DATE = "stock_date";// 30：”2008-01-11″，日期；
	public final static String STOCK_TIME = "stock_time";// 31：”15:05:32″，时间；

	public final static String CHART_MINUTE = "chart_minute";// http://image.sinajs.cn/newchart/min/n/sh000001.gif
	public final static String CHART_DAILY = "chart_daily";// http://image.sinajs.cn/newchart/daily/n/sh601006.gif
	public final static String CHART_WEEKLY = "chart_weekly";// http://image.sinajs.cn/newchart/weekly/n/sh000001.gif
	public final static String CHART_MONTHLY = "chart_monthly";// http://image.sinajs.cn/newchart/monthly/n/sh000001.gif
	public final static String STOCK_CURRENT_CHANGE = "stock_current_change";// 36 变动比例

	public StockDb(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	// 创建table
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "CREATE TABLE " + TABLE_NAME + " (" + STOCK_CODE
				+ " TEXT PRIMARY KEY NOT NULL UNIQUE DEFAULT ( '' ), " + STOCK_NAME + "  TEXT DEFAULT ( '' ), "
				+ STOCK_TODAY_PRICE + " REAL DEFAULT ( 0 ), " + STOCK_LASTDAY_PRICE + " REAL DEFAULT ( 0 ), "
				+ STOCK_CURRENT_PRICE + " REAL DEFAULT ( 0 ), " + STOCK_HEIGEST_PRICE + " REAL DEFAULT ( 0 ), "
				+ STOCK_LOWEST_PRICE + " REAL DEFAULT ( 0 ), " + STOCK_BUY_PRICE0 + " REAL DEFAULT ( 0 ), "
				+ STOCK_SALE_PRICE0 + " REAL DEFAULT ( 0 ), " + STOCK_STADE_TOTAL + " INTEGER, " + STOCK_STADE_MONEY
				+ " REAL DEFAULT ( 0 ), " + STOCK_BUY_PRICE1 + " REAL DEFAULT ( 0 ), " + STOCK_BUY_NUM1
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_BUY_PRICE2 + " REAL DEFAULT ( 0 ), " + STOCK_BUY_NUM2
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_BUY_PRICE3 + " REAL DEFAULT ( 0 ), " + STOCK_BUY_NUM3
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_BUY_PRICE4 + " REAL DEFAULT ( 0 ), " + STOCK_BUY_NUM4
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_BUY_PRICE5 + " REAL DEFAULT ( 0 ), " + STOCK_BUY_NUM5
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_SALE_PRICE1 + " REAL DEFAULT ( 0 ), " + STOCK_SALE_NUM1
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_SALE_PRICE2 + " REAL DEFAULT ( 0 ), " + STOCK_SALE_NUM2
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_SALE_PRICE3 + " REAL DEFAULT ( 0 ), " + STOCK_SALE_NUM3
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_SALE_PRICE4 + " REAL DEFAULT ( 0 ), " + STOCK_SALE_NUM4
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_SALE_PRICE5 + " REAL DEFAULT ( 0 ), " + STOCK_SALE_NUM5
				+ " INTEGER DEFAULT ( 0 ), " + STOCK_DATE + " TEXT DEFAULT ( '' ), " + STOCK_TIME
				+ " TEXT DEFAULT ( '' ), " + CHART_MINUTE + " TEXT DEFAULT ( '' ), " + CHART_DAILY
				+ " TEXT DEFAULT ( '' ), " + CHART_WEEKLY + " TEXT DEFAULT ( '' ), " + CHART_MONTHLY
				+ " TEXT DEFAULT ( '' ), " + STOCK_CURRENT_CHANGE + " REAL DEFAULT ( 0 ));";
		db.execSQL(sql);

		// CREATE TABLE base_table (stock_code TEXT PRIMARY KEY NOT NULL UNIQUE
		// DEFAULT ( '' ),stock_name TEXT DEFAULT ( '' ),stock_lastday_price
		// REAL DEFAULT ( 0 ) );
		// INSERT INTO base_table ("stock_code") VALUES ('sh600001');
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion != newVersion) {
			String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
			db.execSQL(sql);
			onCreate(db);
		}

	}
	public Cursor select() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
		return cursor;
	}

	public long getProfilesCount() {
		SQLiteDatabase db = this.getReadableDatabase();
		long count = DatabaseUtils.queryNumEntries(db, TABLE_NAME);
		db.close();
		return count;
	}

	// 增加操作
	public long insert(String code) {
		SQLiteDatabase db = this.getWritableDatabase();
		/* ContentValues */
		ContentValues cv = new ContentValues();
		cv.put(STOCK_CODE, code);
		long row = db.insert(TABLE_NAME, null, cv);
		return row;
	}

	// 删除操作
	public void delete(String code) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = STOCK_CODE + " = ?";
		String[] whereValue = { code };
		db.delete(TABLE_NAME, where, whereValue);
	}

	// 修改操作
	public void update(String code, String sStockInfo) {
		// var hq_str_sh601006=
		// "大秦铁路,7.060,7.080,7.060,7.090,7.040,7.060,7.070,5000339,35314113.000,1613,7.060,532150,7.050,455000,7.040,314700,7.030,240700,7.020,8600,7.070,183100,7.080,286057,7.090,316900,7.100,128900,7.110,2016-11-24,10:14:03,00";
		// var hq_str_s_sh000001="上证指数,3094.668,-128.073,-3.97,436653,5458126";

		sStockInfo = sStockInfo.substring(sStockInfo.indexOf('=') + 2);
		String[] aryInfo = sStockInfo.split(",");
		// String name = aryInfo[0];
		if (aryInfo.length < 32)
			return;

		SQLiteDatabase db = this.getWritableDatabase();
		String where = STOCK_CODE + " = ?";
		String[] whereValue = { code };

		ContentValues cv = new ContentValues();
		cv.put(STOCK_NAME, aryInfo[0]);
		cv.put(STOCK_TODAY_PRICE, Double.valueOf(aryInfo[1]));
		cv.put(STOCK_LASTDAY_PRICE, Double.valueOf(aryInfo[2]));
		cv.put(STOCK_CURRENT_PRICE, Double.valueOf(aryInfo[3]));

		cv.put(STOCK_HEIGEST_PRICE, Double.valueOf(aryInfo[4]));
		cv.put(STOCK_LOWEST_PRICE, Double.valueOf(aryInfo[5]));
		cv.put(STOCK_BUY_PRICE0, Double.valueOf(aryInfo[6]));
		cv.put(STOCK_SALE_PRICE0, Double.valueOf(aryInfo[7]));
		cv.put(STOCK_STADE_TOTAL, Integer.valueOf(aryInfo[8]));
		cv.put(STOCK_STADE_MONEY, Double.valueOf(aryInfo[9]));
		cv.put(STOCK_BUY_NUM1, Integer.valueOf(aryInfo[10]));
		cv.put(STOCK_BUY_PRICE1, Double.valueOf(aryInfo[11]));
		cv.put(STOCK_BUY_NUM2, Integer.valueOf(aryInfo[12]));
		cv.put(STOCK_BUY_PRICE2, Double.valueOf(aryInfo[13]));
		cv.put(STOCK_BUY_PRICE3, Double.valueOf(aryInfo[15]));
		cv.put(STOCK_BUY_NUM3, Integer.valueOf(aryInfo[14]));
		cv.put(STOCK_BUY_PRICE4, Double.valueOf(aryInfo[17]));
		cv.put(STOCK_BUY_NUM4, Integer.valueOf(aryInfo[16]));
		cv.put(STOCK_BUY_PRICE5, Double.valueOf(aryInfo[19]));
		cv.put(STOCK_BUY_NUM5, Integer.valueOf(aryInfo[18]));
		cv.put(STOCK_SALE_PRICE1, Double.valueOf(aryInfo[21]));
		cv.put(STOCK_SALE_NUM1, Integer.valueOf(aryInfo[20]));
		cv.put(STOCK_SALE_PRICE2, Double.valueOf(aryInfo[23]));
		cv.put(STOCK_SALE_NUM2, Integer.valueOf(aryInfo[22]));
		cv.put(STOCK_SALE_PRICE3, Double.valueOf(aryInfo[25]));
		cv.put(STOCK_SALE_NUM3, Integer.valueOf(aryInfo[24]));
		cv.put(STOCK_SALE_PRICE4, Double.valueOf(aryInfo[27]));
		cv.put(STOCK_SALE_NUM4, Integer.valueOf(aryInfo[26]));
		cv.put(STOCK_SALE_PRICE5, Double.valueOf(aryInfo[29]));
		cv.put(STOCK_SALE_NUM5, Integer.valueOf(aryInfo[28]));
		cv.put(STOCK_DATE, aryInfo[30]);
		cv.put(STOCK_TIME, aryInfo[31]);

		cv.put(CHART_MINUTE, "http://image.sinajs.cn/newchart/min/n/" + code + ".gif");
		cv.put(CHART_DAILY, "http://image.sinajs.cn/newchart/daily/n/" + code + ".gif");
		cv.put(CHART_WEEKLY, "http://image.sinajs.cn/newchart/weekly/n/" + code + ".gif");
		cv.put(CHART_MONTHLY, "http://image.sinajs.cn/newchart/monthly/n/" + code + ".gif");

		Double lastday_price = Double.valueOf(aryInfo[2]);
		Double price = Double.valueOf(aryInfo[3]);
		Double change = (lastday_price != 0 ? (price * 100 / lastday_price) : 100) - 100;

		NumberFormat ddf2 = NumberFormat.getNumberInstance();
		ddf2.setRoundingMode(RoundingMode.HALF_UP);
		ddf2.setMaximumFractionDigits(2);
		String strChg = ddf2.format(change);

		cv.put(STOCK_CURRENT_CHANGE, strChg);
		long row = db.update(TABLE_NAME, cv, where, whereValue);

	}
//ReentrantLock是一个基于AQS的可重入的互斥锁，
//公平锁将确保等待时间最长的线程优先获取锁，将会使整体的吞吐量下降
//非公平锁将不能确定哪一个线程将获取锁，可能会导致某些线程饥饿。
	private final ReentrantLock mLock = new ReentrantLock(true);

	public void lock() {
		mLock.lock();// 多线程安全
	}

	public void unlock() {
		mLock.unlock();
	}

}
